package com.lanju.dao;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.lanju.entity.House;
import com.lanju.entity.HouseFlag;

@Repository
public class HouseFlagDao extends BaseDao<HouseFlag,String>{
    
    @PersistenceContext(unitName = "financialmarketingUnit")
    private EntityManager entityManager;
    
	public  List<HouseFlag> getAllFlagList() {
		StringBuffer sql=new StringBuffer();
		sql.append("from HouseFlag");
		Query query=entityManager.createQuery(sql.toString());
		List<HouseFlag> list=query.getResultList();
		return list;
	}

	public void insertFlagType(Map<String, Object> param) {
		StringBuffer sql = new StringBuffer("insert into lj_flagtype ");
		sql.append(" (flagType_id,flagType_name,creator,status)");
		sql.append(" values("+param.get("id")+","+param.get("name")+","+param.get("userId")+","+param.get("status")+")");
	    this.getSessionFactory().getCurrentSession().createSQLQuery(sql.toString()).executeUpdate();
	}
	
	public void delFlagType(Map<String, Object> param) {
		StringBuffer sql = new StringBuffer("delete from lj_flagtype ");
		sql.append(" where flagType_id = "+param.get("id")+"");
	    this.getSessionFactory().getCurrentSession().createSQLQuery(sql.toString()).executeUpdate();
	}

	public void updateFlagType(Map<String, Object> param) {
		StringBuffer sql = new StringBuffer("update lj_flagtype ");
		
		 if(!StringUtils.isEmpty(param.get("name"))) {
			 sql.append(" set flagType_name = "+param.get("name")+"");
		   }
		 if(!StringUtils.isEmpty(param.get("status"))) {
			 sql.append(" ,status = "+param.get("status")+"");
		   }
		 
		sql.append(" where flagType_id = "+param.get("id")+"");
	    this.getSessionFactory().getCurrentSession().createSQLQuery(sql.toString()).executeUpdate();
	}
	
	public List<Map<String,Object>> queryFlagTypeList(Map<String, Object> param) {
			int offset = Integer.parseInt(param.get("offset").toString());
		   int limit = Integer.parseInt(param.get("limit").toString());
		   
		   StringBuffer sql=new StringBuffer();
		   sql.append("select * from lj_flagtype  where 1=1");
		   if(!StringUtils.isEmpty(param.get("name"))) {
				 sql.append(" and flagType_name like '%"+param.get("name")+"%'");
			   }
		   if(!StringUtils.isEmpty(param.get("status"))) {
				 sql.append(" and status = "+param.get("status")+"");
			   }
		   sql.append(" order by createTime desc") ;
		   SQLQuery query  = this.getSessionFactory().getCurrentSession().createSQLQuery(sql.toString());
		   return query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).setFirstResult(offset).setMaxResults(limit).list();
	}

	public long countFlagTypeList(Map<String, Object> param) {
		StringBuffer sql=new StringBuffer();
		   sql.append("select count(*) from lj_flagtype  where 1=1");
		   if(!StringUtils.isEmpty(param.get("name"))) {
				 sql.append(" and flagType_name like '%"+param.get("name")+"%'");
			   }
		   if(!StringUtils.isEmpty(param.get("status"))) {
				 sql.append(" and status = "+param.get("status")+"");
			   }
		   sql.append(" order by createTime desc") ;
		   SQLQuery query  = this.getSessionFactory().getCurrentSession().createSQLQuery(sql.toString());
		   return query.list().size();
	}
}
